from service.handle_management import HandleManageMent
# from service.user_func import get_current_user
from pao_python.pao.service.security.security_utility import PermissionUtility
from app.permission import Permission
from service.user_func import User_Server


def register(jsonrpc, db_addr, db_name, db_port, session, inital_password, web_path, upload_file):
    handle_manage = HandleManageMent(db_addr, db_port, db_name)
    Permission_utility = PermissionUtility(db_addr, db_port, db_name, session)
    User_func = User_Server(db_addr, db_port, db_name,
                            inital_password, session)
    PERMISSIOM = '有此权限'

    @jsonrpc.method('ISignService.get_all_list')
    def get_all_list_service(condition, page, size):
        '''获取全部全部标识列表'''
        # 权限判断
        def pass_permission():
            # user_list = User_func.get_current_user_by_account_id(session['account_id'])
            # condition['user_id'] = user_list['id']
            if 'handle_type' in condition.keys():
                new_condition = {'$in': condition['handle_type']}
                condition['handle_type'] = new_condition
            res = handle_manage.get_company_list(condition, page, size)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.AllSign_Select)

    @jsonrpc.method('ICompanyTemplateSignService.is_insert')
    def is_insert_service(account):
            # 获取user_id
        res = {}
        user = User_func.get_user_by_acount(account)
        if len(user) > 0:
            user_id = user[0]['id']
            res = handle_manage.is_insert(user_id)
        return res

    @jsonrpc.method('ICompanyTemplateSignService.update_company')
    def update_company_service(data, company):
        '''新增/编辑标识企业'''
        def pass_permission():
            # 企业管理员-用户user_id
            if 'account' in company:
                user_id = User_func.get_user_by_acount(
                    company['account'])[0]['id']
                data['user_id'] = user_id
                company['user_id'] = user_id
            res = handle_manage.update_company(data, 'company', company)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CompanyTemplate_Add)

    @jsonrpc.method('ICompanyTemplateSignService.insert_preview')
    def insert_preview_service(data, company_mes={}):
        '''企业新建预览'''
        # 获取用户id
        # user_list = User_func.get_current_user_by_account_id(session['account_id'])
        # data['user_id'] = user_list['id']
        res = handle_manage.insert_preview(data, 'company')
        return res

    @jsonrpc.method('ICompanyTemplateSignService.get_company_list')
    def get_company_list_service(condition, page, size):
        '''企业标识列表查询-all'''
        def pass_permission():
            condition['handle_type'] = 'company'
            res = handle_manage.get_company_list(condition, page, size)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.AllSign_Select)

    @jsonrpc.method('ICompanyTemplateSignService.get_company')
    def get_company(user_id):
        '''获取企业信息'''
        def pass_permission():
            res = handle_manage.get_company(user_id)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CompanyTemplate_Select_Company)

    @jsonrpc.method('ICompanyTemplateSignService.get_company_sign')
    def get_company_sign_service(handle_id):
        '''获取企业标识信息'''
        def pass_permission():
            res = handle_manage.get_sign_mes(handle_id, 'product_type')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CompanyTemplate_Select_Company)

    @jsonrpc.method('ICompanyTemplateSignService.get_category_list')
    def company_get_category_list_service(condition, page=None, size=None):
        '''某企业所属的品类标识'''
        def pass_permission():
            condition['handle_type'] = 'category'
            res = handle_manage.get_reference_handle_list(
                'product_type', condition, page, size)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)

    @jsonrpc.method('ICompanyTemplateSignService.get_single_list')
    def company_get_single_list_list_service(condition, page=None, size=None):
        '''某企业所属的单品标识'''
        def pass_permission():
            condition['handle_type'] = 'single_product'
            res = handle_manage.get_reference_handle_list(
                'product_type', condition, page, size)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.SingleProduct_Select)

    @jsonrpc.method('ICompanyTemplateSignService.get_externam_link_list')
    def company_get_single_list_service(condition, page=None, size=None):
        '''某企业所属的外链标识'''
        def pass_permission():
            condition['handle_type'] = 'external_links'
            res = handle_manage.get_reference_handle_list(
                'industry_type', condition, page, size)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ExternalLinks_Select)

    @jsonrpc.method('ICategoryTemplateSignService.insert')
    def update_company_service(data):
        '''品类标识新建'''
        def pass_permission():
            if 'id' not in data.keys():
                user_list = User_func.get_current_user_by_account_id(
                    session['account_id'])
                data['user_id'] = user_list['id']
            res = handle_manage.update_company(data, 'category')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)

    @jsonrpc.method('ICategoryTemplateSignService.insert_preview')
    def insert_preview_service(data):
        '''品类新建预览'''
        res = handle_manage.insert_preview(data, 'category')
        return res

    @jsonrpc.method('ICompanyTemplateSignService.get_current_user_company_sign')
    def company_get_current_user_company_sign_service():
        '''获取当前用户企业标识'''
        def pass_permission():
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            res = handle_manage.get_current_user_company_sign(user_list['id'])
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CompanyTemplate_Select_Company)

    @jsonrpc.method('ICategoryTemplateSignService.get_category_list')
    def get_category_list_service(condition, page=None, size=None):
        '''品类标识列表查询-all'''
        def pass_permission():
            condition['handle_type'] = 'category'
            res = handle_manage.get_company_list(condition, page, size)
            return res
        # 平台管理员
        if Permission_utility.is_permission(Permission.AllSign_Select) == PERMISSIOM:
            return Permission_utility.is_peimission_pass(pass_permission, Permission.AllSign_Select)
        else:  # 企业管理员
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            condition['user_id'] = user_list['id']
            return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)

    '''获取品类标识信息'''
    @jsonrpc.method('ICategoryTemplateSignService.get_category')
    def get_category_sign_service(handle_id):
        def pass_permission():
            res = handle_manage.get_sign_mes(handle_id, 'product_type')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)

    @jsonrpc.method('ICategoryTemplateSignService.get_single_list')
    def category_get_single_list(condition, page=None, size=None):
        '''品类所属单品查询'''
        def pass_permission():
            condition['handle_type'] = 'single_product'
            res = handle_manage.get_reference_handle_list(
                'product_type', condition, page, size)
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.SingleProduct_Select)

    @jsonrpc.method('ISingleProductSignService.insert')
    def update_company_service(data):
        '''单品标识新建/编辑'''
        def pass_permission():
            # 新建
            if 'id' not in data.keys():
                user_list = User_func.get_current_user_by_account_id(
                    session['account_id'])
                data['user_id'] = user_list['id']
            res = handle_manage.update_company(data, 'single_product')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.SingleProduct_Select)

    @jsonrpc.method('ISingleProductSignService.insert_preview')
    def insert_preview_service(data):
        '''单品新建预览'''
        res = handle_manage.insert_preview(data, 'single_product')
        return res

    @jsonrpc.method('ISingleProductSignService.get_single_list')
    def get_single_list_service(condition, page, size):
        '''单品标识列表查询-all'''
        def pass_permission():
            condition['handle_type'] = 'single_product'
            res = handle_manage.get_company_list(condition, page, size)
            return res

        if Permission_utility.is_permission(Permission.AllSign_Select) == PERMISSIOM:
            return Permission_utility.is_peimission_pass(pass_permission, Permission.AllSign_Select)
        else:
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            condition['user_id'] = user_list['id']
            return Permission_utility.is_peimission_pass(pass_permission, Permission.SingleProduct_Select)

    @jsonrpc.method('ISingleProductSignService.get_single')
    def get_single_sign_service(handle_id):
        '''查询单品标识信息'''
        def pass_permission():
            res = handle_manage.get_sign_mes(handle_id, 'product_type')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.SingleProduct_Select)

    @jsonrpc.method('ISingleProductSignService.get_category_list')
    def single_get_category_list(condition, page=None, size=None):
        '''单品引用品类查询'''
        def pass_permission():
            condition['handle_type'] = 'category'
            res = handle_manage.get_reference_handle_list(
                'product_type', condition, page, size, 'single_product')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)

    @jsonrpc.method('IExternalLinkSignService.insert')
    def external_link_insert_service(data):
        '''新建外链标识'''
        def pass_permission():
            # 新建
            if 'id' not in data.keys():
                user_list = User_func.get_current_user_by_account_id(
                    session['account_id'])
                data['user_id'] = user_list['id']
            res = handle_manage.update_company(data, 'external_links')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ExternalLinks_Select)

    '''获取跳转标识信息'''
    @jsonrpc.method('IExternalLinkSignService.get_external_link')
    def get_external_link_service(handle_id):
        def pass_permission():
            res = handle_manage.get_sign_mes(handle_id, '')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ExternalLinks_Select)

    @jsonrpc.method('IExternalLinkSignService.get_external_link_list')
    def get_external_link_list_service(condition, page, size):
        '''外链标识列表查询'''
        def pass_permission():
            condition['handle_type'] = 'external_links'
            res = handle_manage.get_company_list(condition, page, size)
            return res
        if Permission_utility.is_permission(Permission.AllSign_Select) == PERMISSIOM:
            return Permission_utility.is_peimission_pass(pass_permission, Permission.AllSign_Select)
        else:
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            condition['user_id'] = user_list['id']
            return Permission_utility.is_peimission_pass(pass_permission, Permission.ExternalLinks_Select)

    # 品类删除
    @jsonrpc.method('ICategoryTemplateSignService.delete_category')
    def delete_category_service(handle_id):
        def pass_permission():
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            res = handle_manage.delete_handle(handle_id, user_list['id'])
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)

    # 单品删除
    @jsonrpc.method('ISingleProductSignService.delete_single')
    def delete_single_service(handle_id):
        def pass_permission():
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            res = handle_manage.delete_handle(handle_id, user_list['id'])
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.SingleProduct_Select)

    # 跳转标识删除
    @jsonrpc.method('IExternalLinkSignService.delete_external_link')
    def delete_external_link_service(handle_id):
        def pass_permission():
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            res = handle_manage.delete_handle(handle_id, user_list['id'])
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.ExternalLinks_Select)

    @jsonrpc.method('ICategoryTemplateSignService.is_allow_delete')
    def is_allow_delete_service(handle_id):
        res = handle_manage.is_allow_delete(handle_id)
        return res

    @jsonrpc.method('ICategoryTemplateSignService.xingtongyuan_login')
    def xingtongyuan_login_service():
        res = handle_manage.xingtongyuan_login()
        return res

    @jsonrpc.method('ICompanyTemplateSignService.get_qr_code')
    def get_qr_code(handle_code, use_logo=None, logo=None):
        user_list = User_func.get_current_user_by_account_id(
            session['account_id'])
        res = handle_manage.create_qr_code(
            web_path, upload_file, handle_code, use_logo, logo, user_list['id'])
        return res

    @jsonrpc.method('ICompanyTemplateSignService.get_single_category_list')
    def get_single_category_list(account):
        res = {}
        user = User_func.get_user_by_acount(account)
        if len(user) > 0:
            user_id = user[0]['id']
            print(user_id, 'user_id')
            res = handle_manage.get_single_category_list(user_id)
        return res

    # 产品标识新增
    @jsonrpc.method('IProductSignService.insert')
    def product_sign_insert(data):
        '''新建产品标识'''
        def pass_permission():
            # 新建
            if 'id' not in data.keys():
                user_list = User_func.get_current_user_by_account_id(
                    session['account_id'])
                data['user_id'] = user_list['id']
            res = handle_manage.update_company(data, 'product_sign')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.Product_Select)

    # 产品标识预览
    @jsonrpc.method('IProductSignService.insert_preview')
    def insert_preview_product(data):
        '''产品标识新建预览'''
        res = handle_manage.insert_preview(data, 'product_sign')
        return res

    # 产品标识删除
    @jsonrpc.method('IProductSignService.delete')
    def delete_product_service(handle_id):
        def pass_permission():
            user_list = User_func.get_current_user_by_account_id(
                session['account_id'])
            res = handle_manage.delete_handle(handle_id, user_list['id'])
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.Product_Select)

    # 产品标识详情查询
    @jsonrpc.method('IProductSignService.get_product')
    def get_product_service(handle_id):
        def pass_permission():
            res = handle_manage.get_sign_mes(handle_id, 'product_type')
            return res
        return Permission_utility.is_peimission_pass(pass_permission, Permission.CategoryTemplate_Select)
